//
//  WelcomeViewController.swift
//  weibo
//
//  Created by 吴婷婷 on 15/11/8.
//  Copyright © 2015年 wutingting. All rights reserved.
//

import UIKit
import SDWebImage

class WelcomeViewController: UIViewController {
    
    //MARK: - 生命周期方法
    //设置界面，视图层次结构
    override func loadView() {
        
        view = backImageView
        
        setupUI()
    }
    //设置数据
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        
        iconView.sd_setImageWithURL(UserAccountViewModel.sharedUserAccount.avatarURL, placeholderImage: UIImage(named: "avatar_default_big"))
    }
    //设置通话
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        iconView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(view.snp_bottom).offset(-view.bounds.height + 200)
        }
        
        UIView.animateWithDuration(1.2, delay: 0,
            usingSpringWithDamping: 0.8,
            initialSpringVelocity: 10,
            options: [],
            animations: { () -> Void in
                
                self.view.layoutIfNeeded()
            }) { (_) -> Void in
                
                UIView.animateWithDuration(0.8, animations: { () -> Void in
                    self.welcomeLabel.alpha = 1
                    }, completion: { (_) -> Void in
//                        print("OK")
                        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootViewControllerNotification, object: nil)
                        
                })
                
                
                
        }
    }
    //MARK: - 懒加载控件
    //背景
    private lazy var backImageView: UIImageView = UIImageView(imageName: "ad_background")
    //头像
    private lazy var iconView: UIImageView = {
        
        let iv = UIImageView(imageName: "avatar_default_big")
        
        iv.layer.cornerRadius = 45
        iv.layer.masksToBounds = true
        
        return iv
    }()
    //文字
    private lazy var welcomeLabel: UILabel = UILabel(title:"欢迎回来" , fontSize: 18)

   

}

extension WelcomeViewController {
    
    private func setupUI() {
        view.addSubview(iconView)
        view.addSubview(welcomeLabel)
        
        welcomeLabel.alpha = 0
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-200)
            make.width.equalTo(90)
            make.height.equalTo(90)
            
        }
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_bottom).offset(16)
            make.centerX.equalTo(view.snp_centerX)
        }
    }
}
